//
//  WSOrderStateView.m
//  FD
//
//  Created by xiaofang wang on 2018/10/31.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  订单状态自定义view 4个label(已下单、制作中、派送中、已完成)
//h 100
#import "WSOrderStateView.h"
@interface WSOrderStateView()
/**label数组*/
@property (nonatomic, strong) NSMutableArray *labeArrM;
@end
@implementation WSOrderStateView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self setupView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame stateType:(WSOrderStateViewType)stateType
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self setupViewWithStateType:stateType];
    }
    return self;
}

- (void)setupViewWithStateType:(WSOrderStateViewType)stateType
{
    //设置背景色：浅灰色
    self.backgroundColor = kMainLightGrayColor;
    
    NSString *yxdStr = NSLocalizedString(L_Order_YiXiaDan, @"已下单");
    NSString *zzzStr = NSLocalizedString(L_Order_ZhiZuoZhong, @"制作中");
    NSString *pszStr = NSLocalizedString(L_Order_PaiSongZhong, @"派送中");
    NSString *ywcStr = NSLocalizedString(L_Order_YiWanCheng, @"已完成");
    NSArray *titleArr = @[yxdStr,zzzStr,pszStr,ywcStr];
    
    if (stateType == WSOrderStateViewTypeRefund) {
        titleArr = @[@"取消订单",@"退款中",@"退款成功"];
    }
    
    CGFloat margin = 10;
    CGFloat sw = self.bounds.size.width;
    CGFloat sh = self.bounds.size.height;
    
    
    CGFloat w = (sw - margin*5) / titleArr.count; //每一个的宽：左右间距，中间间距
    //2019-2-18 三个按钮的情况
    if (stateType == WSOrderStateViewTypeRefund) {
        w = (sw - margin*4) / titleArr.count; //每一个的宽：左右间距，中间间距
    }
    
    
    CGFloat h = 50;
    CGFloat y = (sh - h) * 0.5;
    //创建四个按钮
    for (int i = 0; i<titleArr.count; i++) {
        UILabel *lb = [self createALabel];
        lb.tag  = i; //设置tag值
        lb.text = titleArr[i];//设置文字
        CGFloat x = margin + (w + margin)*i;
        lb.frame = CGRectMake(x, y, w, h);
        //添加到view上
        [self addSubview:lb];
        
        //存储一下所有的lb
        [self.labeArrM addObject:lb];
    }
}

- (UILabel *)createALabel {
    UILabel *lb = [[UILabel alloc] init];
    
    lb.numberOfLines = 0 ; //自动换行
    lb.textAlignment = NSTextAlignmentCenter; //水平居中
    lb.layer.cornerRadius = 5;
    lb.layer.masksToBounds = YES;
    //边框
    lb.layer.borderColor = [UIColor lightGrayColor].CGColor; //边框颜色
    lb.layer.borderWidth = 1.0;
    lb.textColor = kLightGrayTextColor; //灰色文字颜色

    lb.backgroundColor = [UIColor whiteColor]; //背景色
    return lb;
}

#pragma mark - geter
- (NSMutableArray *)labeArrM {
    if (!_labeArrM) {
        _labeArrM = [NSMutableArray array];
    }
    return _labeArrM;
}


#pragma mark - 请求订单状态
/** 2.请求订单状态数据*/
- (void)requestOrderStatusWithOrderId:(NSString *)orderId status:(NSNumber *)status
{

    NSInteger zhuangTai = [status integerValue];
    
//    zhuangTai = 6;
    BOOL b14 = (zhuangTai >0 && zhuangTai <=4) ? YES : NO;
    BOOL b68 = (zhuangTai >=6 && zhuangTai <= 8) ? YES : NO;
    
    if (b14) {
        [self loadOrderStateDataWithOrderId:orderId statusArr:@[@1,@2,@3,@4]];
    }else if (b68) {
        [self loadOrderStateDataWithOrderId:orderId statusArr:@[@6,@7,@8]];
    }else {
        FDLog(@"无状态，不处理");
    }
}

- (void)loadOrderStateDataWithOrderId:(NSString *)orderId statusArr:(NSArray <NSNumber *> *)statusArr
{
    [FDHttpAPi requestUserOrderStatusWithOrderId:orderId Status:statusArr successBlock:^(id responseObjc) {
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) { //请求成功，解析数据
            
            NSArray *dataArr = responseObjc[@"data"];
            
            //解析数据
            [self updateOrderStateContentWithDataArr:dataArr];
            
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}

#pragma mark - 更新订单状态
- (NSArray *)fakeData {
    NSArray *dataArr = @[@{@"status":@0,@"createTime":@"1539831355000"},
                         @{@"status":@1,@"createTime":@"1541153599000"}];
    
    return dataArr;
}

- (void)updateOrderStateContentWithDataArr:(NSArray *)dataArr {
    
    for (int i=0; i<dataArr.count; i++) {
        UILabel *lb = self.labeArrM[i];
        NSString *origingText = lb.text; //取出原来lb中的文字
        
        lb.font = [UIFont systemFontOfSize:10.0*kScaleFit];
        lb.textColor = [UIColor whiteColor];
        lb.backgroundColor = kMainRedColor; //浅红色
        
        //修改label 的文字转态
        NSDictionary *dic = dataArr[i];
        
        NSString *timeStamp  = dic[@"createTime"];
        NSString *timeStr = [self timeStrFromTimeStamp:timeStamp];
        NSString *title = [NSString stringWithFormat:@"%@\n%@",origingText,timeStr];
        lb.text = title;
    }
}


//时间戳转换时间
- (NSString *)timeStrFromTimeStamp:(NSString *)timeStamp {
    
    NSTimeInterval interval = [timeStamp floatValue] / 1000 ; // java 返回的是13位时间戳
    //时间戳转时间
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    
    //日期格式化对象:nyrsf
    NSDateFormatter *fmtnyrsf = [[NSDateFormatter alloc] init];
    [fmtnyrsf setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDateFormatter *fmtnyr = [[NSDateFormatter alloc] init];
    [fmtnyr setDateFormat:@"yyyy-MM-dd"];

    
    //利用日期格式化对象，将日期转换为字符串
    NSString *dateStringnyrsf  = [fmtnyrsf stringFromDate:date];
    NSString *dateStringnyr  = [fmtnyr stringFromDate:date];
    
    
    NSRange nyrRange = [dateStringnyrsf rangeOfString:dateStringnyr];
    NSString *sf = [dateStringnyrsf substringFromIndex:nyrRange.length];
    NSString *resultStr = [NSString stringWithFormat:@"%@\n%@",dateStringnyr,sf];
    //将字符串 添加换行符号
    /*
    2018-11-1 10:31
    2018-11-1
    10:31
    */
    return resultStr;
}



@end
